const fs = require('fs'); 
const deleteall = function (path) {
    let files = [];
    if(fs.existsSync(path)) {
      if(fs.statSync(path).isDirectory()){
        files = fs.readdirSync(path);
        files.forEach(function (file, index) {
            let curPath = path + "/" + file;
            if(fs.statSync(curPath).isDirectory()) {
                deleteall(curPath);
            } else {
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
      }else{
        fs.unlinkSync(path);
      }   
    }
};
const findFile = function (path, findName) {
    let filesAll = [];
    if (fs.existsSync(path)) {
        filesAll = fs.readdirSync(path);
        filesAll.forEach((fileItem,index) => {
            let findCurrPath = path + '/' + fileItem;
            //console.log('当前path',findCurrPath,'---',findName)
            if (fileItem == findName) {
                deleteall(findCurrPath);
                findFile(path,findName);
            } else {
              if(fs.statSync(findCurrPath).isDirectory()) {
                findFile(findCurrPath,findName);
              } 
            }
        })
    }
};
function DeletePlugin (path,fileName) {
    this.path = path;
    this.fileName = fileName;
}
DeletePlugin.prototype.apply = function (compiler) {
    var that = this;
    compiler.hooks.done.tap('DeletePlugin', function (compilation) {
        findFile(that.path,that.fileName)
    })
}

module.exports = DeletePlugin